<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="员工姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入员工姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="员工账号" prop="loginName">
        <el-input
          v-model="queryParams.loginName"
          placeholder="请输入员工账号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="员工编号" prop="userNo">
        <el-input
          v-model="queryParams.userNo"
          placeholder="请输入员工编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="isActive">
        <el-select v-model="queryParams.isActive" clearable size="small">
          <el-option
            v-for="dict in userStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门" prop="orgName">
        <el-input
          v-model="queryParams.orgName"
          placeholder="请输入部门"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否休眠" prop="isSleep">
        <el-select v-model="queryParams.isSleep" clearable size="small">
          <el-option
            v-for="dict in userSleepStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <!--
       <el-form-item label="最后登录时间" prop="lastLogonTime">
        <el-input
          v-model="queryParams.lastLogonTime"
          placeholder="请输入最后登录时间"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" size="mini" @click="handleImportUser"
          >同步用户</el-button
        >
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      :data="oaList"
      :default-sort="{ prop: 'holderNames', order: 'descending' }"
      @sort-change="tableSortChange"
    >
      <el-table-column label="序号" type="index" width="55" align="center" />
      <el-table-column
        label="员工姓名"
        align="center"
        prop="userName"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      />
      <el-table-column
        label="员工账号"
        align="center"
        prop="loginName"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      />
      <el-table-column
        label="员工编号"
        align="center"
        prop="userNo"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      />
      <el-table-column
        label="身份证号"
        align="center"
        prop="idCard"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      />
      <el-table-column
        label="状态"
        align="center"
        prop="isActive"
        :formatter="isActiveFormatter"
      />
      <el-table-column
        label="部门"
        align="center"
        prop="orgName"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      />
      <el-table-column
        label="是否休眠"
        align="center"
        prop="isSleep"
        :formatter="isSleepFormatter"
      />
      <el-table-column
        label="最后登录时间"
        align="center"
        prop="lastLogonTime"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      />
      <el-table-column
        label="账号代持人"
        align="center"
        prop="holderNames"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listOA, importUsers } from "@/api/system/oa";
export default {
  name: "OA",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // oa用户信息表格数据
      oaList: [],
      // 查询参数
      queryParams: {
        pageIndex: 1,
        pageSize: 10,
        userName: undefined,
        loginName: undefined,
        userNo: undefined,
        idCard: undefined,
        isActive: undefined,
        orgName: undefined,
        isSleep: undefined,
        lastLogonTime: undefined,
      },
      userStatusOptions: [],
      userSleepStatusOptions: [],
    };
  },
  created() {
    //用户状态
    this.getDicts("oa_user_status").then((response) => {
      this.userStatusOptions = response.data;
    });
    //用户休眠状态
    this.getDicts("oa_user_sleep_status").then((response) => {
      this.userSleepStatusOptions = response.data;
    });

    this.getList();
  },
  methods: {
    handleImportUser() {
      this.loading = true;
      importUsers()
        .then((res) => {
          if (res && res.data === 1) {
            this.msgSuccess("同步成功");
            this.getList();
          } else {
            this.msgError("同步失败");
          }
        })
        .catch(function () {
          this.msgError("同步失败");
        });
    },
    /** 查询雄安协同平台用户信息列表 */
    getList() {
      this.loading = true;
      listOA(this.queryParams).then((response) => {
        this.oaList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    isActiveFormatter(row, column) {
      let isActive = row.isActive;
      var text = "";
      if (isActive == 0 || (isActive != undefined && isActive != "")) {
        this.userStatusOptions.forEach((item) => {
          if (item.dictValue === isActive + "") {
            text = item.dictLabel;
          }
        });
      }
      return text;
    },
    isSleepFormatter(row, column) {
      let isSleep = row.isSleep;
      var text = "";
      if (isSleep == 0 || (isSleep != undefined && isSleep != "")) {
        this.userSleepStatusOptions.forEach((item) => {
          if (item.dictValue === isSleep + "") {
            text = item.dictLabel;
          }
        });
      }
      return text;
    },
    tableSortChange(column) {
      this.queryParams.pageNum = 1;
      if (column.order === "descending") {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "desc";
      } else {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "asc";
      }
      this.getList();
    },
  },
};
</script>